package com.hngates.architecture.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import com.hngates.architecture.domain.*;
import com.hngates.architecture.domain.dto.ProjectBuilderDto;
import com.hngates.architecture.domain.dto.ProjectStatusDto;
import com.hngates.architecture.service.*;
import com.hngates.common.constant.Constants;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author ywentao
 * @version 1.0.0
 * @className ArcHomeController.java
 * @description TODO
 * @createTime 2022年07月06日 13:24:00
 */
@Controller
@RequestMapping("/architecture/home")
public class ArcHomeController extends BaseController {
    private String prefix = "architecture/home";

    @Autowired
    private IArcProjectService arcProjectService;
    @Autowired
    private IArcBuilderCompanyService arcBuilderCompanyService;
    @Autowired
    private IArcConstructContractService arcConstructContractService;
    @Autowired
    private IArcLabourCompanyService arcLabourCompanyService;
    @Autowired
    private IArcLabourContractService arcLabourContractService;
    @Autowired
    private IArcLabourSubCompanyService arcLabourSubCompanyService;
    @Autowired
    private IArcLabourSubCompanyDetailsService arcLabourSubCompanyDetailsService;
    @Autowired
    private IArcWorkerComplainService arcWorkerComplainService;

    @GetMapping("/home")
    public String home() {
        return prefix + "/home";
    }

    @GetMapping("/index")
    public String index() {
        return prefix + "/index";
    }

    @GetMapping("/detail")
    public String detail(Long projectId, Long builderId, ModelMap modelMap) {
        ArcProject project = arcProjectService.selectArcProjectById(projectId);
        ArcBuilderCompany builderCompany = arcBuilderCompanyService.selectArcBuilderCompanyById(builderId);

        modelMap.put("serverDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
        modelMap.put("project", project);
        modelMap.put("builderCompany", builderCompany);
        return prefix + "/detail";
    }

    @GetMapping("/queryProjectCount")
    @ResponseBody
    public AjaxResult queryProjectCount() {
        return AjaxResult.success("ok", arcProjectService.selectProjectCount());
    }

    /***
     * 表格中的项目信息
     * @param pName 项目名
     * @param bName 公司名
     * @return
     */
    @GetMapping("/queryProjectBuilderInfo")
    @ResponseBody
    public AjaxResult queryProjectBuilderInfo(String queryName) {
        List<ProjectBuilderDto> dtoList = arcProjectService.selectProjectBuilderInfos(queryName);
        return AjaxResult.success("ok", dtoList);
    }

    @GetMapping("/queryProjectStatusInfo")
    @ResponseBody
    public AjaxResult queryProjectStatusInfo() {
        List<ProjectStatusDto> dtos = arcProjectService.selectProjectStatus();
        return AjaxResult.success("ok", dtos);
    }

    @GetMapping("/queryBuilderContract")
    @ResponseBody
    public AjaxResult queryBuilderContract(Long projectId, Long builderCompanyId) {
        ArcConstructContract query = new ArcConstructContract();
        query.setBuilderId(builderCompanyId);
        query.setProjectId(projectId);
        List<ArcConstructContract> list = arcConstructContractService.selectArcConstructContractList(query);

        if (list == null || list.isEmpty()) {
            return AjaxResult.error("未查到相关的建设合同信息.");
        }
        //建造合同
        ArcConstructContract constructContract = list.get(0);
        MapBuilder<Object, Object> mapBuilder = MapUtil.builder().put("builderContract", constructContract);

        //查劳务公司
        Long labourId = constructContract.getLabourId();
        ArcLabourCompany labourCompany = arcLabourCompanyService.selectArcLabourCompanyById(labourId);
        mapBuilder.put("labourCompany", labourCompany);

        if (labourCompany != null) {
            //劳务合同
            ArcLabourContract queryLabourContract = new ArcLabourContract();
            queryLabourContract.setLabourId(labourCompany.getId())
                    .setProjectId(projectId);
            List<ArcLabourContract> arcLabourContracts = arcLabourContractService.selectArcLabourContractList(queryLabourContract);
            if (arcLabourContracts != null && !arcLabourContracts.isEmpty()) {
                List<Map> labourContracts = new ArrayList<>();
                for (ArcLabourContract item : arcLabourContracts) {
                    Map<String, Object> comp = new HashMap<>();
                    comp.put("labourContract", item);
                    ArcLabourSubCompany labourSubCompany = arcLabourSubCompanyService.selectArcLabourSubCompanyById(item.getSubLabourId());
                    comp.put("labourSubCompany", labourSubCompany);
                    if (labourSubCompany != null) {
                        //查分包详情
                        ArcLabourSubCompanyDetails subCompanyDetails = arcLabourSubCompanyDetailsService.selectArcLabourSubCompanyDetailsBySubLabourId(item.getSubLabourId());
                        comp.put("labourSubCompanyDetails", subCompanyDetails);

                        //查投诉
                        ArcWorkerComplain arcWorkerComplainQuery = new ArcWorkerComplain();
                        arcWorkerComplainQuery.setProjectId(projectId)
                                .setSubLabourId(item.getSubLabourId());
                        List<ArcWorkerComplain> arcWorkerComplains = arcWorkerComplainService.selectArcWorkerComplainList(arcWorkerComplainQuery);
                        comp.put("workerComplains", arcWorkerComplains);
                    }
                    labourContracts.add(comp);
                }
                mapBuilder.put("labourContractComplexList", labourContracts);
            }
        }
        return AjaxResult.success("ok", mapBuilder.map());

    }

    /**
     * 投诉详情 页面
     *
     * @return
     */
    @GetMapping("/complain")
    public String complain(Long projectId, Long builderId, ModelMap modelMap) {
        ArcProject project = arcProjectService.selectArcProjectById(projectId);
        List<ArcProject> arcProjects = new ArrayList<>();
        arcProjects.add(project);
        modelMap.put("projects", arcProjects);

        ArcLabourSubCompany arcLabourSubCompany = new ArcLabourSubCompany();
        arcLabourSubCompany.setDelFlag(Constants.DEl_FLAG_0);
        List<ArcLabourSubCompany> arcLabourSubCompanies = arcLabourSubCompanyService.selectArcLabourSubCompanyList(arcLabourSubCompany);
        modelMap.put("labourSubs", arcLabourSubCompanies);

        return prefix + "/complain";
    }

    /**
     * 投诉详情 数据
     *
     * @param qy
     * @return
     */
    @PostMapping("/queryComplain")
    @ResponseBody
    public TableDataInfo queryComplain(ArcWorkerComplain qy) {
        startPage();
        qy.setDelFlag(Constants.DEl_FLAG_0);
        List<ArcWorkerComplain> list = arcWorkerComplainService.selectArcWorkerComplainList(qy);
        return getDataTable(list);
    }
}
